/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.service;

import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.ProductCategory;
import com.reddys.rad.inventory.db.Purchase;
import com.reddys.rad.inventory.db.PurchaseDetails;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import com.reddys.rad.inventory.db.PurchaseReturn;
import com.reddys.rad.inventory.db.PurchaseReturnDetails;
import com.reddys.rad.inventory.db.Stock;
import static com.reddys.rad.inventory.service.SalesService.em;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import com.reddys.rad.inventory.utils.Utilities;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import sun.security.ec.ECDHKeyAgreement;


/**
 *
 * @author Administrator
 */
public class PurchaseReturnService {
       static EntityManager em;
       
       public ObservableList getPuschaseList(){
            ObservableList data = FXCollections.observableArrayList();
          em = EntityManagerImpl.getEntityManager();
          Query query=em.createNamedQuery("Purchase.findAll");
          List list= (List) query.getResultList();
         Iterator iter = list.iterator();
         while(iter.hasNext()){
            Purchase purchase = (Purchase) iter.next();
            data.add(purchase);
        }
  
          return data;
       }

    public ObservableList<PurchaseDetails> getPurchaseDetailsList(String purchaseId) {
          ObservableList data = FXCollections.observableArrayList();
          em = EntityManagerImpl.getEntityManager();
          Query query=em.createNamedQuery("PurchaseDetails.findByPurchaseId");
          query.setParameter("purchaseId", purchaseId);
          List list= (List) query.getResultList();
         Iterator iter = list.iterator();
         while(iter.hasNext()){
            PurchaseDetails purchaseDetails = (PurchaseDetails) iter.next();
              Query queryProductCategory = em.createNamedQuery("ProductCategory.findByProductCategoryId");
                queryProductCategory.setParameter("productCategoryId", purchaseDetails.getProductCategoryId());
                List productCategoryList = queryProductCategory.getResultList();
                if(productCategoryList.size()>0){
                    ProductCategory productCategory = (ProductCategory) productCategoryList.get(0);
                    purchaseDetails.setProductCategoryDescription(productCategory.getProductCategoryDescription());
                }
            data.add(purchaseDetails);
        }
        return data;
    }
    
        public Purchase getPurchaseByPurchaseId(String purchaseId) {
          Purchase purchase = null;
          Query query=em.createNamedQuery("Purchase.findByPurchaseId");
          query.setParameter("purchaseId", purchaseId);
          List list= (List) query.getResultList();
          if(list.size()>0){
              purchase = (Purchase) list.get(0);
          }
       return purchase;
    }


    public PurchaseDetails getPurchaseDetailByPurchaseId(String purchaseId, String productId) {
          PurchaseDetails purchaseDetails = null;
          Query query=em.createNamedQuery("PurchaseDetails.findByDetailByPurchaseIdandProductId");
          query.setParameter("purchaseId", purchaseId);
          query.setParameter("productId", productId);
          List list= (List) query.getResultList();
          if(list.size()>0){
              purchaseDetails = (PurchaseDetails) list.get(0);
          }
       return purchaseDetails;
    }

    public static Stock getStock(String productId) {
         ArrayList<Stock> stockList = new ArrayList<Stock>();
         Stock stock = null;
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Stock.findByProductId");
        query.setParameter("productId",productId);
        List list = query.getResultList();
        Iterator iter = list.iterator();
        while(iter.hasNext()){
            stock = (Stock) iter.next();
           // stockList.add(stock);
        }
        return stock;
    }
    
    public void updateStock(Stock stock) {
      EntityManagerImpl.persist(stock);

    }

    public PurchaseReturn saveReturn(PurchaseReturn purchaseReturn, EntityManager em) {
        if(Utilities.isNull(purchaseReturn.getPurchaseReturnId())){
            em.persist(purchaseReturn);
        }else{
            em.merge(purchaseReturn);
        }
        return purchaseReturn;
    }

    public PurchaseReturnDetails saveReturnDetaild(PurchaseReturnDetails purchaseDetails, EntityManager em) {
        if(Utilities.isNull(purchaseDetails.getPurchaseReturnDetailId())){
            em.persist(purchaseDetails);
        }else{
            em.merge(purchaseDetails);
        }
        return purchaseDetails;
    }

    public PurchaseOrderDetails getPurchaseOrderDetailByPurchaseId(String purchaseId, String productId) {
          PurchaseOrderDetails purchaseOrderDetails = null;
          Query queryPurchase = em.createNamedQuery("Purchase.findByPurchaseId");
          queryPurchase.setParameter("purchaseId", purchaseId);
          List list= (List) queryPurchase.getResultList();
          if(list.size()>0){
              Purchase purchase = (Purchase) list.get(0);
              if(purchase!=null){
               Query queryPurchaseOrderDetail = em.createNamedQuery("PurchaseOrderDetails.findByPurchaseOrderIdandProductId");
               queryPurchaseOrderDetail.setParameter("productId", productId);
               queryPurchaseOrderDetail.setParameter("purchaseOrderId", purchase.getPurchaseOrderNo());
                 List purchaseOrderDetailList= (List) queryPurchaseOrderDetail.getResultList();
                 if(purchaseOrderDetailList.size()>0){
                     purchaseOrderDetails = (PurchaseOrderDetails) purchaseOrderDetailList.get(0);
                 }
              }
          }
          return purchaseOrderDetails;  
    }

}
